import { _decorator, Component, EventTouch, Input, instantiate, Node, UITransform, Vec2, Vec3 } from 'cc';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
import { GAME_TOP, GlobalData, LevelConfig, SOUND_TYPE } from '../../../Data/GlobalData';
import { SingleCar } from './SingleCar';
import { Tools } from '../../../Tool/Tools';
import { GunLoad } from '../GunLoad/GunLoad';
import { TopPromptText } from '../../../Top/TopPromptText';
const { ccclass, property } = _decorator;

@ccclass('CarLoad')
export class CarLoad extends BaseComponent {

    //大炮层
    @property(GunLoad)
    private GunLoad: GunLoad = null;

    //关卡层
    private LevelLoad: Node = null;

    //遮罩层
    @property(Node)
    private Mask: Node = null;

    protected Init(): void {
        this.node.on(Input.EventType.TOUCH_START, this.TouchStart, this);
    }


    /**
     * 更新关卡信息
     */
    public UpdateLevelInfo() {

        if (this.LevelLoad) {
            this.LevelLoad.setParent(null);
            this.LevelLoad.destroy();
        }

        GlobalData.CanClickCar = true;
        this.LevelLoad = instantiate(app.resManager.Prefabs["Level" + LevelConfig[GlobalData.CurrentLevel].carpos.toString()]);
        this.Mask.addChild(this.LevelLoad);

        for (let car of this.LevelLoad.children) {
            let buttle_count: number = car.getComponent(SingleCar).BulletCount;
            let car_color: number = car.getComponent(SingleCar).CarColorIndex;

            //随机
            // let car_color: number = Tools.GetRandomNum(0, 5);

            car.getComponent(SingleCar).UpdateColor(car_color);
            car.getComponent(SingleCar).Cb = this.AddGun.bind(this);
            for (let i = 0; i < buttle_count; i++) {
                GlobalData.DragonBodyInfo.push(car_color);
            }
        }
    }

    /**
     * 点击屏幕
     */
    private TouchStart(_e: EventTouch): void {
        if (!GlobalData.CanClickCar) return;

        //通过点击的位置，判断对应的车
        let click_pos: Vec2 = _e.getUILocation();
        let click_pos_world: Vec3 = this.node.getComponent(UITransform).convertToWorldSpaceAR(new Vec3(click_pos.x, click_pos.y, 0));
        for (let car of this.LevelLoad.children) {
            if (car.active) {
                let car_pos: Vec3 = this.node.getComponent(UITransform).convertToWorldSpaceAR(car.getWorldPosition());
                let car_width: number = (car.angle == 0 || car.angle == 180) ? car.getComponent(SingleCar).CarWidth : car.getComponent(SingleCar).CarHeight;
                let car_height: number = (car.angle == 0 || car.angle == 180) ? car.getComponent(SingleCar).CarHeight : car.getComponent(SingleCar).CarWidth;
                if (Tools.isIntersect1(click_pos_world, car_pos, car_width / 2, car_height / 2)) {

                    if (!this.GunLoad.ReturnCanAddGun()) {
                        app.audioManager.PlayEffect(SOUND_TYPE.Button);
                        app.topManager.ShowTop(GAME_TOP.TopPromptText, false);
                        app.topManager.CurrentTop.getComponent(TopPromptText).UpdatePrompt("没有位置了");
                        return;
                    }

                    app.audioManager.PlayEffect(SOUND_TYPE.CarMove);
                    car.getComponent(SingleCar).Click();
                }
            }

        }
    }

    /**
     * 添加炮台
     */
    public AddGun(_gunindex: number, _guncount: number) {
        this.GunLoad.AddGun(_gunindex, _guncount);
    }
}


